<?php 
session_start();
$r_teacher_id=$_SESSION['id'];
$now	= date("Y-m-d");
require_once('../include/connect.inc.php');
if($_SESSION['sess_id'] <> session_id()){
?>
 <SCRIPT LANGUAGE="JavaScript">
			alert("กรุณาเข้าสู่ระบบให้ถูกต้องตามขั้นตอน...!");
			location="login.php";
</script>
<?php }?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Model Test Program</title>
<link rel="stylesheet" type="text/css" href="../template/template_style_wood.css">
</head>
<?php
	$msg="";
	$query = query("SELECT * FROM tb_question 
	Inner Join  tb_type ON  tb_type.type_id = tb_question.type_id where question_id='".$_GET[question_id]."'");
		while ($rs = fetch_array($query)) {
		    $question_id=$rs['question_id'];
			$question_detail=$rs['question_detail'];
			$level_q_id= $rs['level_q_id'];
			$select_cho= $rs['select_cho'];
			$type_id=$rs['type_id'];
			$type_name= $rs['type_name'];
			$pic_name= $rs['pic_name'];
			$media_name= $rs['media_name'];
			}
		if($_GET[op]=="edit"){
		if($pic_name!=""){
			$FileType = $_FILES['pic_name']['type'];//นามสกุล file
			$FileTemp = $_FILES['pic_name']['tmp_name'];//ชื่อ file
			$FileName = $_FILES['pic_name']['name'];//ไฟล์จำลองก่อนจะเอาไปเข้าไฟล์เดอร์ที่เตรียมไว้
			$FileSize = $_FILES['pic_name']['size'];//ขนาดของ file
			$type 	  = array("image/gif","image/jpeg","image/png","image/bmp","image/pjpeg","image/x-png","image/txt");
				if ( !empty($FileName) ) { //ตรวจสอบว่ามี file หรือป่าว
					if ( in_array($FileType,$type) ) { //ตรวจสอบว่าสกุลfile ถูกต้องหรือป่าว
						if( $FileSize/(1024*1024*10) > 1 ) { //ขนาดเกินที่กำหนดไว้หรือป่าว
							echo "เกินขนาด";
							exit();
						}
						$ftype 	   = substr( $FileName, strlen($FileName)-4, 4 );
						$pic_name  = date("Ydm_his").$ftype;  //เปลี่ยนชื่อกันการซ้ำชื่อไฟล์
						if ( ! move_uploaded_file($FileTemp,"../upload/$pic_name") ) {
							echo  "ข้อผิดพลาด : ไม่สามารถ  upload ไฟล์ได้";
							exit();
						}else{
							echo " ";
						}
					}else{
						echo "นามสกุลไฟล์ของคุณไม่ถูกต้อง กรุณาตรวจสอบ";
					}
				}else{
					echo "กรุณาเลือกไฟล์";
			}
	}
	if($media_name!=""){
	if($_FILES['media_name']['name']){
		list($files, $error) = upload('media_name','../upload/','mpeg,mp3');
		if($error){ 
			echo $การเพิ่มไฟล์เสียงผิดพลาด;
			exit();
			}
			$media_name=$files;
		}
	}
		$strSql="update tb_question set question_detail='".$_POST[question_detail]."',pic_name='$pic_name',media_name='$media_name' where question_id='".$_POST[question_id]."'";
		$rs = execute($strSql);
	if(!$rs) {
		$msg = "เกิดข้อผิดพลาดโปรดทำรายการใหม่";
			header("location:frm_edit_question.php");
	} else {
				$strSql20="INSERT INTO tb_edit_question (question_id,r_teacher_id,date) VALUES ('".$_POST[question_id]."','$r_teacher_id','$now')";
					$rs20 = execute($strSql20);
				$query2 = query("SELECT * FROM tb_choice  where question_id ='".$_GET[question_id]."'");
				$i=0; //echo "SELECT * FROM tb_choice  where question_id =". $_GET[question_id] . "<br>";
				$arr[]=$_POST['choice'];
				while ($rs2 = fetch_array($query2)){
				//echo $_POST["choiceid$i"] . " = " . $_POST["choice_detail$i"] . "  : " . $_POST["choice_chk$i"] . "  :$: <br>" ;
				if($type_id==1)
					if($_POST["choiceid$i"]==$_POST['choice1'])
						$answer=1;
					else
						$answer=0;
				
				if($type_id==2)
					if($_POST["choiceid$i"]==$_POST["choice_chk$i"])
						$answer=1;
					else
						$answer=0;
						
				if($type_id==5)
					if($_POST["choiceid$i"]==$_POST["choice1"])
						$answer=$question_id;
					else
						$answer=0;
						
				if($type_id==6)
					if($_POST["choiceid$i"]==$_POST["choice1"])
						$answer=1;
					else
						$answer=0;
					
				$strSql="update tb_choice set 
							  choice_detail ='".$_POST["choice_detail$i"]."' ,
							  answer = $answer
							  where question_id='".$_POST[question_id]."' AND choice_id ='" . $_POST["choiceid$i"] ."'" ;
				$rs = execute($strSql);
				$i+=1;
				}
?>
	<SCRIPT LANGUAGE="JavaScript">
		alert("คุณได้ทำการแก้ไขข้อสอบเรียบร้อยแล้ว");
			location="frm_edit_question.php?question_id=<?php echo $question_id;?>";
	</script>
<?php			
		exit();
	}
}
?>
<body>
<div id="menu"><div><img src="../images/icon-m.png" /></div>
<div id="menulinks">
		<a  href="../person_tea.php">ข้อมูลส่วนตัว</a>
		<a href="../tea_edit.php">เปลี่ยนรหัสผ่านใหม่</a>
        
        <a href="show_subject.php">จัดการรายวิชา</a>
		<a  class="active" href="../teacher_test.php">จัดการข้อสอบ</a>
        <a  href="../teacher_tests.php">จัดการชุดข้อสอบ</a>
        <a  href="../agree_set.php">กำหนดสิทธิ์</a>
		<a href="../exam/tea_chack.php">ตรวจสอบคะแนน</a>
        <a href="../news/manage_news.php">ประกาศข่าวสาร</a>
        <a href="../problem/send_problem.php">แสดงความคิดเห็น</a>
        <a href="../logout.php">ออกจากระบบ</a>
		</div></div>           
 <div id="p3">    
<div id="header"></div>
   <div id="page"><div id="p1">คุณ<?php echo $_SESSION['teacher_name'];?><hr/>
<form action="frm_edit_question.php?op=edit&question_id=<?=$_GET['question_id'];?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
	<table  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <th align="left">แก้ไขข้อสอบประเภท : <font color="#0000FF"><? echo $type_name; ?></font></br></th>
        <th align="right"><a href="frm_show_question.php"><img src="../../images/undo.png" border="0" /></a></th>
      </tr>
    </table> 
          <table width="57%"  border="0" cellpadding="0" cellspacing="0">
            <tr>
             <td colspan="2"><font size="+2">คำถาม:</font><input type="text" name="question_detail" id="question_detail" value="<? echo "$question_detail";?>" size="40" /></td>
            </tr>
             <?php 
             $query2 = query("SELECT * FROM tb_choice  where question_id ='".$_GET[question_id]."'");
			 $i=0;
				while ($rs2 = fetch_array($query2)){
			$choice_id=$rs2['choice_id'];	
			$choice_detail=$rs2['choice_detail'];
			$answer=$rs2['answer'];
             ?>
          <tr>
            	<td width="74%"><div class="p21"><input name="choiceid<?=$i;?>" type="hidden" id="choiceid" value="<?=$choice_id;?>" />
            	<input type="text" name="choice_detail<?=$i;?>" id="choice_detail" value="<? echo "$choice_detail";?>" size="40" /></div></td>
                <td align="left" width="26%">
			
            <?php 
			if($type_id==2){?>
        	 <input type="checkbox" name="choice_chk<?=$i;?>" value="<?php echo $rs2[choice_id];?>" <?php if  ($rs2['answer']==1){?> checked="checked" <?php }?> >
           <?php } else { ?>
             <input type="radio" name="choice1" value="<?php echo $rs2[choice_id];?>" <?php if  ($rs2['answer']==1){?> checked="checked" <? } if  ($rs2['answer']==$question_id){ ?>checked="checked" <?php } ?>>
             <?php  }  ?>			</td>
	<?php  $i+=1; }?>
        </tr>
        <tr>
        	<td>เปลี่ยนรูป :
       	    <input type="file" name="pic_name" id="pic_name" /></td>
        	<td>&nbsp;</td>
        </tr>
        <tr>
        	<td>เปลี่ยนไฟล์เสียง :       	    
       	    <input type="file" name="media_name" id="media_name" /></td>
        	<td>&nbsp;</td>
        </tr>
        
        <?php
		if($pic_name!="")
		{
		?>
        <tr>
        	<td>
			  <div align="left"><a href="../upload/<?php echo  "$pic_name";?>" target="_blank">
				<img src="../upload/<?php echo  "$pic_name";?>" border="0" width=300 height=200 
				onclick="window.open('../upload/<?php echo  "$pic_name";?>','mywindow','width=1000,height=1000')" /></a>
				<div>
    				<small>คลิ๊กที่รูปเพื่อดูขนาดใหญ่</small>				</div>
			  </div>            </td>
        </tr>
        <?php 
		}else{
			echo "";
		}
		?>
        <?php 
		if($media_name!=""){
		?>
	<table align="left">
        <tr>
        	<td>
           	  <th align="left">
        	<object id="mediaPlayer" height="23" width="80" classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95">
            	<param name="fileName" value='<?php echo "../upload/$media_name";?>' valuetype='ref' />
                <param name="AutoStart" value="0" />
				<param name="TransparentAtStart" value="true" />
				<param name="SendPlayStateChangeEvents" value="true" />
				<param name="ShowControls" value="0" />
				<param name="ShowDisplay" value="0" />
				<param name="ShowStatusBar" value="1" />
				<param name="ShowGotoBar" value="0" />
				<param name="ShowPositionControls" value="0" />
				<param name="AutoSize" value="0" />
				<param name="volume" value="0" />
				<param name="loop" value="0" />
			</object>			  </th>
			</td>
		</tr>
        <tr>
        	<td>
              <th align="right">
        	<img style="CURSOR: pointer" 
				onclick="mediaPlayer.play()" height="30" 
				src="../soundbox/play-48x48.png" 
				width="30" /><img style="CURSOR: pointer" 
				onclick="mediaPlayer.pause()" height="30" 
				src="../soundbox/puse-48x48.png" 
				width="30" /><img 
                src="../soundbox/stop-48x48.png" 
				width="30" height="30" style="CURSOR: pointer" 
				onclick="mediaPlayer.stop()" />			  </th>
            </td>
        </tr>
	 </table>
     <?php
     }else{
	 	echo "";
	}
	 ?>
     <?php 
	 $num=0;
	 $query70 = query("SELECT * FROM tb_edit_question where question_id='".$_GET[question_id]."' AND r_teacher_id='$r_teacher_id' ");
	 	while ($rs70 = fetch_array($query70)){
			$aa=$rs70['question_id'];
			$bb=$rs70['r_teacher_id'];
			$cc=$rs70['date'];
			$num++;
	 ?>
     <?php 
	// if($rs70="")
	 ?>
     <tr>
     	<td>
        	แก้ไขครั้งที่ :<?php echo "$num";?>
            <?php 
			$query80 = query("SELECT * FROM tb_teacher where r_teacher_id='$bb' ");
				while ($rs80 = fetch_array($query80)){
					$t_name=$rs80['teacher_fname']." ".$rs80['teacher_lname'];
					}
			?>
         	โดย :<?php echo "คุณ $t_name";?>
        </td>
        <td>
        	วันที่แก้ไข :<?php echo "$cc";?>
        </td>
	</tr>
<?php }?>
 </table>
</br></br></br>
<input type="submit" name="submit" id="submit" value="บันทึก" />
<input type="reset" name="cancel" id="cancel" value="ยกเลิก" />
<input type="hidden" name="question_id" id="question_id" value="<? echo $_GET[question_id]; ?>" />

</form>
    </div>
    </div>
<div id="footer"></div>
</div>
</body>
</html>
